package xyz.weizhi.tanhua.appserver.controller;

import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.commons.utils.JwtUtils;
import xyz.weizhi.tanhua.appserver.interceptor.UserHolder;
import xyz.weizhi.tanhua.appserver.service.UserInfoService;
import xyz.weizhi.tanhua.model.domain.UserInfo;
import xyz.weizhi.tanhua.model.vo.UserInfoVo;

/**
 * @Author milian
 * @Date 2021/10/24 0024 20:42
 */
@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /*
      根据用户id查询用户信息
   */
    @GetMapping
    public ResponseEntity users(Long userID, @RequestHeader("Authorization") String token){
        if (userID==null){
            userID = UserHolder.getId();
        }
        UserInfoVo userInfo = userInfoService.findByid(userID);
        return ResponseEntity.ok(userInfo);
    }
    /*
        更新用户信息
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer)claims.get("id");
        Long UserID = Long.valueOf(id);
        userInfo.setId(UserHolder.getId());
        userInfoService.update(userInfo);
        return null;
    }

}
